package br.brpv.silvestrin.conferencia.webservice;

import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import br.brpv.silvestrin.conferencia.R;
import br.brpv.silvestrin.conferencia.models.User;
import br.brpv.silvestrin.conferencia.webservice.WebserviceManager.WSRequestCompleteListener;

public class TestWebservice extends Webservice {

	TestWebserviceListener caller;

	public TestWebservice(Context context, TestWebserviceListener caller) {
		super(context);
		this.caller = caller;
	}

	public void validateServer(String url) {
		// TODO mudar mensagem
		WebserviceManager ws = new WebserviceManager(context,
				context.getString(R.string.wsmessage_validateserver),
				new ValidateServerListener());
		WebserviceManager.setURL(url);

		try {
			ws.execute(WebserviceManager.Method.HELLO_WORLD, "Hello");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private class ValidateServerListener implements
			WSRequestCompleteListener<SoapObject> {

		public void onTaskComplete(SoapObject result) {
			if (result != null)
				caller.onValidateServer(true);

			else
				caller.onValidateServer(false);
		}

	}

	public void validateUser(String user, String password) {
		WebserviceManager ws = new WebserviceManager(context,
				context.getString(R.string.wsmessage_validateuser),
				new ValidateUserListener());

		try {
			ws.execute(WebserviceManager.Method.VALIDA_USUARIO, user, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private class ValidateUserListener implements
			WSRequestCompleteListener<SoapObject> {

		public void onTaskComplete(SoapObject result) {
			if (result == null) {
				// TODO avisar que nao conseguiu acessar o servidor
			}
			boolean valid = false;

			if (result != null)
				valid = result.getProperty(0).toString().equals("true");

			caller.onValidateUser(valid);
		}
	}

	public interface TestWebserviceListener {
		public void onValidateServer(boolean isValid);

		public void onValidateUser(boolean isValid);
	}

}
